import React, { useEffect, useState } from 'react'
import { Button, Card, Form, Input, message, Space } from 'antd'
import {addHospitalSet,getHospitalSetById,updateHospitalSet} from '@api/hospital/hospitalSet'
import { useNavigate, useParams } from 'react-router-dom'
import { HospitalSet } from '@/api/hospital/model/hospitalSetTypes'
export default function AddOrUpdateHospital() {
    let [form] = Form.useForm()
    let [loading, setLoading] = useState<boolean>(false)
    let navigate = useNavigate()
    let {id} = useParams()
    
    async function onFinish() {
        try{
            let values = form.getFieldsValue()
            setLoading(true)
            if(id){
                await updateHospitalSet({
                    ...values,
                    id
                })
                message.success('编辑成功')
            }else{
                await addHospitalSet(values)
                message.success('添加成功')
            }
            setLoading(false)
            navigate('/syt/hospital/hospitalSet')
        }catch(e){
            setLoading(false)
        }
    }
    useEffect(()=>{
        async function main(){
            let res = await getHospitalSetById(id as string)
            form.setFieldsValue(res)
            console.log(res)
        }
        main()
    },[])
    return (
        <Card>
            <Form
                form={form}
                name="basic"
                labelCol={{ span: 2 }}
                wrapperCol={{ span: 22 }}
                initialValues={{ remember: true }}
                onFinish={onFinish}
                autoComplete="off"
            >
                <Form.Item
                    label="医院名称"
                    name="hosname"
                    rules={[{ required: true, message: '请输入医院名称!' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="医院编号"
                    name="hoscode"
                    rules={[{ required: true, message: '请输入医院编号!' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="api基础路径"
                    name="apiUrl"
                    rules={[{ required: true, message: '请输入api基础路径' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="联系人姓名"
                    name="contactsName"
                    rules={[{ required: true, message: '请输入联系人姓名!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="联系人手机"
                    name="contactsPhone"
                    rules={[
                        { required: true, message: '请输入联系人手机!' },
                        { pattern:/^1[3-9]\d{9}$/, message:'手机号不合法'}
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item wrapperCol={{ offset: 2, span: 16 }}>
                    <Space size={30}>
                        <Button type="primary" htmlType="submit" loading={loading}>
                            保存
                        </Button>
                        <Button >
                            返回
                        </Button>
                    </Space>
                </Form.Item>
            </Form>
        </Card>
    )
}
